package copyengine.ui.component.list.old.dataprovider
{
	import copyengine.ui.component.list.old.cellrender.ICECellRenderData;

	public class CEMultipleRowDataProvider implements ICEListDataProvider
	{
		private var dataProvider:Vector.<ICECellRenderData>;

		private var cellLength:int;

		public function CEMultipleRowDataProvider()
		{
			dataProvider = new Vector.<ICECellRenderData>();
		}

		public function clear():void
		{
			dataProvider.splice(0, dataProvider.length);
		}

		public function getData():Vector.<ICECellRenderData>
		{
			return dataProvider;
		}

		public function pushData(_data:ICECellRenderData):void
		{
			dataProvider.push(_data);
		}

		public function deleteData(_data:ICECellRenderData):void
		{
			for (var i:int = 0; i < dataProvider.length; i++)
			{
				if (_data == dataProvider[i])
				{
					dataProvider.splice(i, 1);
					break;
				}
			}
		}

		public function forEach(_forEachFunction:Function):void
		{
			dataProvider.forEach(_forEachFunction);
		}

		public function setDataVector(_dataVector:Vector.<ICECellRenderData>):void
		{
			dataProvider.splice(0, dataProvider.length);
			if (_dataVector != null)
			{
				while (_dataVector.length > 0)
				{
					dataProvider.push(_dataVector.pop());
				}
			}
		}

		public function filter(_filterFunction:Function):Vector.<ICECellRenderData>
		{
			return dataProvider.filter(_filterFunction);
		}

		public function sort(_sortFunction:Function):void
		{
			dataProvider.sort(_sortFunction);
		}


		public function setCellLength(_length:int):void
		{
			cellLength = _length;
		}

		public function getDataByIndex(_groupIndex:int, _cellIndex:int):ICECellRenderData
		{
			if (_cellIndex < cellLength && _groupIndex < getGroupLength())
			{
				var index:int = getInternalIndex(_groupIndex, _cellIndex);
				if (index < dataProvider.length)
				{
					return dataProvider[index];
				}
			}
			return null;
		}

		public function getGroupIndexByData(_data:ICECellRenderData):int
		{
			for (var i:int = 0; i < dataProvider.length; i++)
			{
				if (dataProvider[i] == _data)
				{
					return Math.floor(i / cellLength);
				}
			}
			return int.MIN_VALUE;
		}

		public function forEachDataInGroup(_groupIndex:int, _handleFunction:Function):void
		{
			for (var i:int = 0; i < cellLength; i++)
			{
				var data:Object = getDataByIndex(_groupIndex, i);
				if (data != null)
				{
					_handleFunction.call(data);
				}
				else
				{
					break;
				}
			}
		}

		public function getGroupLength():int
		{
			return Math.ceil(dataProvider.length / cellLength);
		}

		public function getCellLength():int
		{
			return cellLength;
		}

		public function dispose():void
		{
//			dataProvider.length = 0;
			dataProvider = null;
		}

		public function getInternalIndex(_groupIndex:int, _cellIndex:int):int
		{
			return _groupIndex * cellLength + _cellIndex;
		}


	}
}
